package com.sn.springboot_netty.controller;

import com.sn.springboot_netty.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Author fyy
 * @Description
 * @Date 2020/5/28 15:27
 */
@Controller
public class FileDownController {


    private static final String zipPath = "";

    /**
     * @Author fyy
     * @Description 打包文件
     * @Date 2020/5/28 15:56
     */
    @RequestMapping(value = "/fileToZip",method = RequestMethod.GET)
    @ResponseBody
    public void fileToZip(HttpServletResponse response) throws Exception {
        String imgUrl = "http://39.101.190.227:8090/img/visu/alarm/862353040333163_20200528171228_0A_0_0_res.jpg,http://39.101.190.227:8090/img/visu/alarm/1590524815036_alarm.jpg,http://39.101.190.227:8090/img/visu/alarm/1590535530992_alarm.jpg";
        String folderPath = DateUtil.dateFormat(new Date(),DateUtil.DATE_TIME_STRING_PATTERN); //新建文件夹存储本次图片
        String newFileName = "500KV郑汉线";
        String[] imgurls = imgUrl.split(",");
        for (String url : imgurls) {
            ImageUtil.makeImage(url,folderPath,newFileName+ UUID.randomUUID().toString());
        }
        boolean flag = ZipUtil.folderToZip(zipPath+"/"+folderPath, zipPath, folderPath);
        if(flag){
            FileDownloadUtil.downFile(response,folderPath);
        }
    }

}
